import os
import sys
import time
import yaml
from rich.console import Console
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
import core.rprint as rprint

console = Console()

with open('./config.yaml','r', encoding='utf-8') as f:
    config = yaml.load(f, Loader=yaml.CLoader)

def vulscan(date):
    rprint.info(date, '正在调用Nuclei进行资产漏洞探测任务！')
    time.sleep(1)
    target = 'result/domain.txt'
    if config['system'] == 1:
        if config['pocupdate'] == True:
            update = 'y'
        elif config['pocupdate'] == False:
            update = console.input("[[bold green]" + date + "[/bold green]] [bold red]FuYao[/bold red] [[bold red]error[/bold red]] [bold yellow]是否进行漏洞库更新？Y or N：[/bold yellow]")

        if update == 'Y' or update =='y':
            rprint.info(date, '正在更新Nuclei漏洞库！')
            os.system("./core/plus/mac/nuclei -silent -ut -ud 'core/pocdata'")
            rprint.info(date, '正在使用Nuclei漏扫节点进行漏洞扫描！')
            os.system("./core/plus/mac/nuclei -l %s -t ./core/pocdata/ -s low,medium,high,critical  -rate-limit 500 -bulk-size 250 -concurrency 250 -silent -stats -si 10 -retries 2 -me result/%s/"%(target, date +  "_pocscan"))
        elif update == 'N' or update =='n':
            rprint.info(date, '正在使用Nuclei漏扫节点进行漏洞扫描！')
            os.system("./core/plus/mac/nuclei -l %s -t ./core/pocdata/ -s low,medium,high,critical  -rate-limit 500 -bulk-size 250 -concurrency 250 -silent -stats -si 10 -retries 2 -me result/%s/"%(target, date +  "_pocscan"))
    if config['system'] == 2:
        if config['pocupdate'] == True:
            update = 'y'
        elif config['pocupdate'] == False:
            update = console.input("[[bold green]" + date + "[/bold green]] [bold red]FuYao[/bold red] [[bold red]error[/bold red]] [bold yellow]是否进行漏洞库更新？Y or N：[/bold yellow]")
        
        if update == 'Y' or update =='y':
            rprint.info(date, '正在更新Nuclei漏洞库！')
            os.system("./core/plus/linux/nuclei -silent -ut -ud 'core/pocdata'")
            rprint.info(date, '正在使用Nuclei漏扫节点进行漏洞扫描！')
            os.system("./core/plus/linux/nuclei -l %s -t ./core/pocdata/ -s low,medium,high,critical  -rate-limit 500 -bulk-size 250 -concurrency 250 -silent -stats -si 10 -retries 2 -me result/%s/"%(target, date +  "_pocscan"))
        elif update == 'N' or update =='n':
            rprint.info(date, '正在使用Nuclei漏扫节点进行漏洞扫描！')
            os.system("./core/plus/linux/nuclei -l %s -t ./core/pocdata/ -s low,medium,high,critical  -rate-limit 500 -bulk-size 250 -concurrency 250 -silent -stats -si 10 -retries 2 -me result/%s/"%(target, date +  "_pocscan"))